package com.kkb.robotcustomerservice.controller;

import com.kkb.robotcustomerservice.entities.JsonResult;
import com.kkb.robotcustomerservice.entities.QuestionTemplate;
import com.kkb.robotcustomerservice.entities.Questions;
import com.kkb.robotcustomerservice.service.QuestionsService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author HXH
 */
@RestController
@RequestMapping("/chat")
@CrossOrigin
public class QuestionsController {

    @Autowired
    private QuestionsService questionsService;


    /**
     * 获取问题列表
     */
    @GetMapping("/queryDefaultQuestions")
    public JsonResult<List<Questions>> queryDefaultQuestions(){
        List<Questions> questions = questionsService.getQuestions();
        JsonResult<List<Questions>> jsonResult = new JsonResult<>();
        //获取失败，列表为空
        if (questions.size() == 0) {
            jsonResult.setCode(0);
            jsonResult.setMsg("问题列表为空");
            jsonResult.setSuccess(false);
            jsonResult.setData(null);
            return jsonResult;
        }
        //查询列表成功
        jsonResult.setCode(1);
        jsonResult.setMsg("获取问题列表成功");
        jsonResult.setSuccess(true);
        jsonResult.setData(questions);
        return jsonResult;
    }

    /**
     * 根据上传的xmls文件进行批量删除操作
     * @author hefuren
     */
    @PostMapping("/upload")
    public JsonResult<Object> batchDeleteByFile(@RequestParam("file") MultipartFile file) {
        System.out.println("[文件类型] - [{}]"+ file.getContentType());
        System.out.println("[文件名称] - [{}]"+ file.getOriginalFilename());
        System.out.println("[文件大小] - [{}]"+ file.getSize());

        JsonResult res = new JsonResult();
        //解析文件得到question集合
        try {
            List<QuestionTemplate> questions = questionsService.parseFile(file);
            //测试解析结果
            System.out.println(questions);
            res.setMsg("success");
            res.setCode(200);
            res.setSuccess(true);
        } catch (IOException e) {
            e.printStackTrace();
            res.setSuccess(false);
            //TODO 根据不同的情况设置不同的返回码
        }

        return res;
    }
}
